package com.ruoyi.web.petmall.petback.controller;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.web.petmall.entity.UserEntity;
import com.ruoyi.web.petmall.petback.service.UserManageService;
import com.ruoyi.web.petmall.uitl.SevenCowCloudUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping(value ="/petBack/userManage")
@Api(tags = "个人管理")
public class UserManageController extends BaseController {


    @Autowired
    private UserManageService userManageService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询个人信息
     * @param userEntity 查询条件
     * @return 用户信息
     */
    @ApiOperation("查询个人信息")
    @GetMapping("/getUserInfo")
    public UserEntity getUserInfo(UserEntity userEntity) {
        return userManageService.getUserInfo(userEntity);
    }

    /**
     * 上传图片
     * @param file 用户头像文件
     * @return 上传结果
     * @throws IOException 文件操作异常
     */
    @ApiOperation(value ="上传图片", notes ="上传图片", httpMethod ="POST")
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public AjaxResult upload(@ApiParam(value = "用户头像", required = true) @RequestPart("file") MultipartFile file) throws IOException {
        // 判断文件是否为空
        if (file.isEmpty()) {
            return AjaxResult.error("图片不能为空");
        }
        // 上传到七牛云
        Object msg = SevenCowCloudUtil.QiniuCloudUploadImage(file);
        UserEntity userEntity = new UserEntity();
        userEntity.setUserPicture(msg.toString());
        AjaxResult ajax = AjaxResult.success();
        // 返回信息放入数据
        ajax.put("data", msg);
        return ajax;
    }

    /**
     * 修改个人信息，包括用户头像
     * @param userEntity 用户信息
     * @return 操作结果
     */
    @ApiOperation("修改个人信息")
    @PostMapping("/updateUserInfo")
    public AjaxResult updateUserInfo(@RequestBody UserEntity userEntity) {
        // 获取缓存中的用户信息
        LoginUser loginUser = getLoginUser();

        // 获取密码
        String mpassword = userEntity.getPassword();
        // 密码加密
        String newPassword = SecurityUtils.encryptPassword(mpassword);
        // 将密码存入实体类
        userEntity.setUserMpassword(mpassword);
        userEntity.setPassword(newPassword);

        if (userManageService.updateUserInfo(userEntity) > 0) {
            // 更新缓存用户密码
            loginUser.getUser().setPassword(newPassword);
            tokenService.setLoginUser(loginUser);
            return AjaxResult.success();
        }
        return AjaxResult.error("修改异常，请联系管理员");
    }
}
